<%@ page import="java.util.List" %>
<%@ page import="com.onlinevideo.entity.Authority" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <link href="../static/css/bootstrap.min.css" rel="stylesheet">
    <link href="../static/css/bootstrap-theme.min.css" rel="stylesheet">
    <script src="../static/js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script src="../static/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="../static/js/sweetalert.min-2.1.2.js" type="text/javascript"></script>
    <style type="text/css">
        th {
            text-align: center;
        }

        td {
            text-align: center;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            $("#add").click(function () {
                var authorityURL = $("#addAuthorityURL").val();
                var authorityDescription = $("#addAuthorityDescription").val();
                $.ajax({
                    url: "/authority.do",
                    data: {
                        url: authorityURL,
                        authorityDescription: authorityDescription,
                        action: "insert"
                    },
                    dataType: "text",
                    success: function (data) {
                        var result = JSON.parse(data)
                        if (result.code == "1") {
                            //4.向用户响应数据（成功，失败）
                            swal("提示", result.message, "success").then(function (isOk) {});
                        } else {
                            swal("警告", result.message, "error").then(function (isOk) {});
                        }
                    },
                    error: function (data) {
                        console.log(data);
                    }
                })
            });
            $("#edit").click(function () {
                var authorityId = $(this).attr("data-id");
                var authorityURL = $("#editAuthorityURL").val();
                var authorityDescription = $("#editAuthorityDescription").val();
                $.ajax({
                    url: "/authority.do",
                    data: {
                        id:authorityId,
                        url: authorityURL,
                        authorityDescription: authorityDescription,
                        action: "update"
                    },
                    dataType: "text",
                    success: function (data) {
                        var result = JSON.parse(data)
                        if (result.code == "1") {
                            //4.向用户响应数据（成功，失败）
                            swal("提示", result.message, "success").then(function (isOk) {});
                        } else {
                            swal("警告", result.message, "error").then(function (isOk) {});
                        }
                    },
                    error: function (data) {
                        console.log(data);
                    }
                })
            });
            $("body").on("click",".update",function () {
                var object = $(this).parents("tr");
                $("#editAuthorityURL").val(object.children("th:eq(1)").text().trim());
                $("#editAuthorityDescription").val(object.children("th:eq(2)").text().trim());
                $("#edit").attr("data-id",$(this).attr("data-id"));
            });
            $("body").on("click",".delete",function (){
                var $this = $(this);
                var authorityId = $(this).attr("data-id");
                $.ajax({
                    url: "../authority.do",
                    type:"post",
                    data:{
                        "id":authorityId,
                        "action":"delete"
                    },
                    success:function (data) {
                        var result = JSON.parse(data)
                        if (result.code == "1") {
                            swal("提示", result.message, "success").then(function (isOk) {
                                $this.parents("tr").remove();
                            });
                        } else {
                            swal("警告", result.message, "error").then(function (isOk) {});
                        }
                    }
                });
            });
        });
    </script>
    <style type="text/css">
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="bs-example" data-example-id="btn-variants">
            <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#addAuthorityBtn">新增</button>
            <div class="add">
                <div class="modal fade" id="addAuthorityBtn" tabindex="-1" role="dialog" aria-labelledby="addAuthorityLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="addAuthorityLabel">新增权限信息</h4>
                            </div>
                            <div class="modal-body">
                                <div class="row">
                                    <div class="col-md-2"></div>
                                    <div class="col-md-8">
                                        <form class="form-horizontal" action="../user.do?action=insert">
                                            <div class="form-group">
                                                <label for="addAuthorityURL" class="col-sm-3 control-label">权限URL:</label>
                                                <div class="col-sm-9">
                                                    <input type="text" class="form-control" id="addAuthorityURL" placeholder="请输入权限URL">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="addAuthorityDescription" class="col-sm-3 control-label">权限说明:</label>
                                                <div class="col-sm-9">
                                                    <textarea id="addAuthorityDescription"  class="form-control" placeholder="权限说明" style="resize: none"></textarea>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <div class="row">
                                    <div class="col-md-2"></div>
                                    <div class="col-md-8">
                                        <div class="col-md-4">
                                            <button type="button" class="btn btn-primary opera" id="add">添加</button>
                                        </div>
                                        <div class="col-md-4">
                                            <button type="reset" class="btn btn-default">重置</button>
                                        </div>
                                        <div class="col-md-4">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="edit">
                <div class="modal fade" id="editAuthorityBtn" tabindex="-1" role="dialog" aria-labelledby="editAuthorityLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="editAuthorityLabel">修改权限信息</h4>
                            </div>
                            <div class="modal-body">
                                <div class="row">
                                    <div class="col-md-2"></div>
                                    <div class="col-md-8">
                                        <form class="form-horizontal" action="../authority.do?action=update">
                                            <div class="form-group">
                                                <label for="editAuthorityURL" class="col-sm-3 control-label">权限URL:</label>
                                                <div class="col-sm-9">
                                                    <input type="text" class="form-control" id="editAuthorityURL" placeholder="请输入权限URL">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="editAuthorityDescription" class="col-sm-3 control-label">权限说明:</label>
                                                <div class="col-sm-9">
                                                    <textarea id="editAuthorityDescription"  class="form-control" placeholder="权限说明" style="resize: none"></textarea>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <div class="row">
                                    <div class="col-md-2"></div>
                                    <div class="col-md-8">
                                        <div class="col-md-4">
                                            <button type="button" class="btn btn-primary opera" id="edit" data-id="-1">修改</button>
                                        </div>
                                        <div class="col-md-4">
                                            <button type="reset" class="btn btn-default">重置</button>
                                        </div>
                                        <div class="col-md-4">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">取消
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-striped" style="margin-bottom: 0;">
                <thead>
                <tr style="text-align: center ; vertical-align:center;">
                    <th>编号</th>
                    <th>可访问地址</th>
                    <th>权限说明</th>
                </tr>
                </thead>
                <tbody>
                <%
                    List<Authority> authorities =( List<Authority>) request.getAttribute("authorities");
                    for (int i = 0; i < authorities.size(); i++) {
                        Authority authority = authorities.get(i);
                %>
                <tr style="vertical-align:middle;text-align: center">
                    <th scope="row">
                        <%= authority.getId()%>
                    </th>
                    <th>
                        <%= authority.getUrl()%>
                    </th>
                    <th>
                        <%= authority.getAuthorityDescription()%>
                    </th>
                    <th>
                        <button type="button" class="btn btn-primary btn-sm update" data-toggle="modal" data-id="<%= authority.getId() %>" data-target="#editAuthorityBtn" id="editInfo">修改</button>
                        <button type="button" class="btn btn-danger  btn-sm delete" data-id="<%= authority.getId()%>">删除</button>
                    </th>
                </tr>
                <%
                    }
                %>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
